package com.ruralflow.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 创建序列编号
 */
@Slf4j
@Component
public class SequenceUtils {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 创建序列编号
     * @param key 序列前半部分（包含固定开头如OTS）
     * @return 序列编号
     */
    public String createSequenceNumber(String key) {

        String dateTime = CommonDateUtils.dateTime();
        Long increment = redisTemplate.opsForValue().increment(key + dateTime, 1);
        if (increment == 1) {
            redisTemplate.expire(key + dateTime, 1, TimeUnit.DAYS);
        }
        String format = String.format("%06d", increment);
        return key + dateTime + format;
    }



}
